﻿using System;
using System.Collections.Generic;
using System.Data;
using eTMS.Controllers.LopService;
namespace eTMS.Controllers
{
    public class LopController : LopControllerBase
    {
        public LopController()
            : base()
        {
        }
        public static List<LopEntity> FindLopByNganhHoc(int NganhHoc_ID, List<LopEntity> lsLop)
        {
            return lsLop.FindAll(delegate(LopEntity oFind) { return oFind.NganhHoc_ID == NganhHoc_ID; });
        }
        public static LopEntity FindLopByTenLop(string TenLop, List<LopEntity> lsLop)
        {
            return lsLop.Find(delegate(LopEntity oFind) { return oFind.TenLop.Trim().ToLower() == TenLop.Trim().ToLower(); });
        }
        public static List<LopEntity> LoadLopByNganhHoc_IDKhoaHoc_ID(int NganhHoc_ID, int KhoaHoc_ID)
        {
            LopService.LopServiceClient lopServiceClient = new LopServiceClient();
            return new List<LopEntity>(lopServiceClient.SelectAllFilter("NganhHoc_ID=" + NganhHoc_ID.ToString() + " AND KhoaHoc_ID=" + KhoaHoc_ID.ToString()));
        }
        public static List<LopEntity> LoadLopByKhoaHoc_ID( int KhoaHoc_ID)
        {
            LopService.LopServiceClient lopServiceClient = new LopServiceClient();
            return new List<LopEntity>(lopServiceClient.SelectAllFilter(" KhoaHoc_ID=" + KhoaHoc_ID.ToString()));
        }
        public void LoadAllLopWithSoSinhVienGiaoVienByNganhHoc_IDKhoaHoc_IDKhoa_ID(int NganhHoc_ID, int KhoaHoc_ID, int Khoa_ID)
        {
            Table = dao.SelectAllDSLopWithSoSinhVienGiaoVienByKhoaHocNganhHocKhoa(KhoaHoc_ID, NganhHoc_ID, Khoa_ID).Tables[0];
            AddIndexToTable();
        }
        public List<LopEntity> LoadAllLopByListMaLopNganhHocKhoaHoc(List<string> lsMaLop, int NganhHoc_ID, int KhoaHoc_ID)
        {
            return new List<LopEntity>(dao.SelectAllFilter("MaLop IN('" + string.Join("','", lsMaLop.ToArray()) + "') AND NganhHoc_ID=" + NganhHoc_ID.ToString() + " AND KhoaHoc_ID=" + KhoaHoc_ID.ToString()));
        }
        public List<LopEntity> LoadAllLopByNganhHoc_IDKhoaHoc_ID(int NganhHoc_ID, int KhoaHoc_ID)
        {
            return new List<LopEntity>(dao.SelectAllFilter("NganhHoc_ID=" + NganhHoc_ID.ToString() + " AND KhoaHoc_ID=" + KhoaHoc_ID.ToString()));
        }
        public bool CheckExistsMaLop(List<string> lsMaLop, int NganhHoc_ID, int KhoaHoc_ID)
        {
            List<LopEntity> ls = LoadAllLopByListMaLopNganhHocKhoaHoc(lsMaLop, NganhHoc_ID, KhoaHoc_ID);
            return ls.Count > 0;
        }
        public void AddRangeLop(List<string> lsMaLop, int NganhHoc_ID, int KhoaHoc_ID, int SoSVToiDa)
        {
            foreach (string maLop in lsMaLop)
            {
                ResetObject();
                LopObject.TenLop = maLop;
                LopObject.MaLop = maLop;
                LopObject.SoSinhVienToiDa = SoSVToiDa;
                LopObject.NganhHoc_ID = NganhHoc_ID;
                LopObject.KhoaHoc_ID = KhoaHoc_ID;
                LopObject.GiaoVienPhuTrach_ID = 0;
                LopObject.MoTaLop = "";
                AddNew();
            }
        }
        public void UpdateLop()
        {
            List<LopEntity> lsLop = new List<LopEntity>();
            foreach (DataRow row in Table.Rows)
            {
                lsLop.Add(MapDataRowToObject(row));
            }
            dao.UpdateList(lsLop.ToArray());
        }
    }
}
